package com.china08.yxyapi.api.schwebsite.index;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.sws.SwsSection;
import com.china08.yxyapi.entity.db.sws.SwsSettingDomains;
import com.china08.yxyapi.entity.mg.sws.SwsArticle;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.SwsSectionRepository;
import com.china08.yxyapi.repository.db.SwsSettingDomainsRepository;
import com.china08.yxyapi.repository.mg.SwsArticleRepository;
import com.china08.yxyapi.util.DateToolUtils;
import com.china08.yxyapi.util.ThumborUtil;

/**
 * 学校网站首页:新闻咨询:文章列表
 * 
 * @author Ertao.Fang
 *
 */
@CrossOrigin
@RestController("SwsIndexArticleListController")
public class SwsIndexArticleListController extends YxyApiBaseController {

    @Autowired
    private SwsArticleRepository swsArticleRepository;

    @Autowired
    private SwsSettingDomainsRepository swsSettingDomainsRepository;

    @Autowired
    private SwsSectionRepository swsSectionRepository;

    @RequestMapping(value = "/api_yw/schwebsite/index/article/srh", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public List<RespModel> process(HttpServletRequest request, HttpServletResponse response,
            @RequestBody(required = false) ReqModel reqModel) {
        String domainName = getDomainName(request);

        Assert.isTrue(StringUtils.isNotBlank(domainName), "域名为空");
        // 通过域名获取学校ID
        SwsSettingDomains settingDomains = swsSettingDomainsRepository.findByDomainName(domainName);
        Assert.isTrue(null != settingDomains, "域名对象为空");

        String schoolId = settingDomains.getSchoolId();
        RespModel model = null;
        boolean recommend = true;
        String sectionId = "";
        List<SwsArticle> orderByCreatedDateDesc = swsArticleRepository
                .findTop6ByAuditStatusAndOwnerSchoolIdAndSection1IdNotAndRecommendOrderByShowDateDesc(1, schoolId,
                        sectionId, recommend);
        Assert.isTrue(null != orderByCreatedDateDesc, "文章列表为空");
        List<RespModel> list = new ArrayList<RespModel>();
        for (SwsArticle swsArticle : orderByCreatedDateDesc) {
            model = new RespModel();
            model.id = swsArticle.getId();
            if (StringUtils.isNotBlank(swsArticle.getFaceImg())) {
                model.faceImg = swsArticle.getFaceImg().substring(5, swsArticle.getFaceImg().length());
            } else {
                model.faceImg = "";
            }

            if (StringUtils.isBlank(model.faceImg)) {
                // 获取详情中的第一张图片作为推荐图片
                Document document = Jsoup.parse(swsArticle.getContent());
                Elements elements = document.select("img");
                if (elements.size() > 0) {
                    model.faceImg = elements.get(0).attr("src");
                }

            }
            if (StringUtils.isNotBlank(swsArticle.getFaceImg())) {
                String substring = swsArticle.getFaceImg().substring(swsArticle.getFaceImg().lastIndexOf(".") + 1);
                if (substring.equals("gif") || substring.equals("GIF")) {
                    model.faceImg_550_330 = swsArticle.getFaceImg();
                    model.faceImg_500_152 = swsArticle.getFaceImg();
                    model.faceImg_500_202 = swsArticle.getFaceImg();
                    model.faceImg_140_88 = swsArticle.getFaceImg();
                    model.faceImg_540_202 = swsArticle.getFaceImg();
                    model.faceImg_242_142 = swsArticle.getFaceImg();
                } else {
                    model.faceImg_550_330 = ThumborUtil.schoolWebSiteResize(swsArticle.getFaceImg(), 550, 330);
                    model.faceImg_500_152 = ThumborUtil.schoolWebSiteResize(swsArticle.getFaceImg(), 500, 152);
                    model.faceImg_500_202 = ThumborUtil.schoolWebSiteResize(swsArticle.getFaceImg(), 500, 202);
                    model.faceImg_140_88 = ThumborUtil.schoolWebSiteResize(swsArticle.getFaceImg(), 140, 88);
                    model.faceImg_540_202 = ThumborUtil.schoolWebSiteResize(swsArticle.getFaceImg(), 540, 202);
                    model.faceImg_242_142 = ThumborUtil.schoolWebSiteResize(swsArticle.getFaceImg(), 242, 142);
                }

            } else {
                model.faceImg_550_330 = "";
                model.faceImg_500_152 = "";
                model.faceImg_500_202 = "";
                model.faceImg_140_88 = "";
                model.faceImg_540_202 = "";
                model.faceImg_242_142 = "";
            }

            String html = swsArticle.getContent();
            model.content = Jsoup.parse(html).text();
            model.title = swsArticle.getTitle();
            model.showDate = DateToolUtils.format(swsArticle.getShowDate(), "MM-dd");
            String section1Id = swsArticle.getSection1Id();
            String section2Id = swsArticle.getSection2Id();
            model.section1Id = swsArticle.getSection1Id();
            if (StringUtils.isBlank(swsArticle.getSection2Id())) {
                model.section2Id = "0";
            } else {
                model.section2Id = swsArticle.getSection2Id();
            }
            if (StringUtils.isNotBlank(section1Id)) {
                SwsSection section2 = swsSectionRepository.findById(section1Id);
                if (null != section2) {
                    model.sectionName = section2.getSectionName();
                }
            } else {
                SwsSection section2 = swsSectionRepository.findById(section2Id);
                if (null != section2) {
                    model.sectionName = section2.getSectionName();
                }
            }

            list.add(model);
        }
        return list;
    }

    protected static class ReqModel extends YxyApiReqModel {

    }

    protected static class RespModel extends YxyApiRespModel {

        public String id;

        public String faceImg;

        public String faceImg_550_330;

        public String faceImg_500_152;

        public String faceImg_500_202;

        public String faceImg_540_202;

        public String faceImg_140_88;

        public String faceImg_242_142;

        public String title;

        public String content;

        public String sectionName;

        public String showDate;

        public String section1Id;

        public String section2Id;

    }
}